package com.groupbuying.payproducer.payCenterStrategy.iml;

import com.groupbuying.payproducer.connector.SysconfigFeign;
import com.groupbuying.payproducer.constants.CommonConstants;
import com.groupbuying.payproducer.constants.PayCenterTypeConstants;
import com.groupbuying.payproducer.constants.PayInfoEnumConstants;
import com.groupbuying.payproducer.constants.PayInfoSearchConstants;
import com.groupbuying.payproducer.dao.PayInfoDao;
import com.groupbuying.payproducer.domain.PayInfoDO;
import com.groupbuying.payproducer.domain.PayOrderRelDO;
import com.groupbuying.payproducer.service.PayOrderRelService;
import com.groupbuying.payproducer.payCenterStrategy.PayStrategy;
import com.groupbuying.payproducer.utils.apiresult.ApiResult;
import com.groupbuying.payproducer.wxpay.WXPay;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 原生微信支付
 * @Author Vincentxin
 * @Date 2019-05-20
 */
@Component
public class NativeWeChatPayStrategy implements PayStrategy {
    @Autowired
    private PayInfoDao payInfoDao;

    @Autowired
    private SysconfigFeign sysconfigFeign;

    @Autowired
    private PayOrderRelService payOrderRelService;
    @Override
    public ApiResult unifiedPayment(Map<String, String> paramMap, PayCenterTypeConstants type) {
        if (StringUtils.isEmpty(paramMap.get("openId"))){
            return ApiResult.failure("参数错误");
        }
        PayInfoDO payInfoDO = new PayInfoDO();
        payInfoDO.setId(sysconfigFeign.getPayID());
        payInfoDO.setIsDelete(CommonConstants.COMMON_FLAG_UNDEL);
        payInfoDO.setGmtCreate(new Date());
        payInfoDO.setGmtModified(new Date());
        payInfoDO.setPayState(PayInfoEnumConstants.ENUM_PAYSTATE_UNPAY);
        payInfoDO.setPayType(PayInfoEnumConstants.ENUM_PAYTYPE_WX);
        payInfoDO.setPayChannel(PayInfoEnumConstants.ENUM_PAYCHANNEL_OR);
        /**
         * 需要注意获取金额的key
         */
        payInfoDO.setPayMoney(new BigDecimal(paramMap.get("payCash")));
        payInfoDO.setPayBalance(new BigDecimal(0));
        payInfoDO.setPayCur(new BigDecimal(0));
        payInfoDao.save(payInfoDO);

        //生成订单关联
        PayOrderRelDO payOrderRelDO = new PayOrderRelDO();
        payOrderRelDO.setIsDelete(CommonConstants.COMMON_FLAG_UNDEL);
        /**
         * 需要注意获取订单编号key
         */
        payOrderRelDO.setOrderId(paramMap.get("orderId"));
        payOrderRelDO.setId(payInfoDO.getId());
        payOrderRelDO.setGmtCreate(new Date());
        payOrderRelDO.setGmtModified(new Date());
        payOrderRelService.save(payOrderRelDO);

        //生成微信预支付回执
        Map wxMap = new HashMap();
        wxMap.put(PayInfoSearchConstants.SEARCH_ID, payInfoDO.getId());
        /**
         * 参数中openid的key以及验证
         */
        wxMap.put("open_id", paramMap.get("openId"));
        wxMap.put(PayInfoSearchConstants.SEARCH_PAYMONEY, payInfoDO.getPayMoney().toString());
        return this.weiXinPrePaymentMsg(wxMap);

    }


    public ApiResult weiXinPrePaymentMsg(Map<String, String> paramsMap) {
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "请核实是否为本人订单，谨防被骗");
        data.put("out_trade_no", paramsMap.get(PayInfoSearchConstants.SEARCH_ID));
        data.put("openid", paramsMap.get("open_id"));
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", paramsMap.get(PayInfoSearchConstants.SEARCH_PAYMONEY));
        //此处指定为扫码支付
        data.put("trade_type", "JSAPI");
        data.put("product_id", "12");
        try {
            Map<String, String> resp = WXPay.unifiedOrder(data);
            System.out.println(resp);
            return ApiResult.success(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ApiResult.failure();
    }
}
